<template>
  <el-dialog
      v-model="planEditWinDialog"
      title="编辑计划"
      width="50%">
    <template #default>
      <el-form :model="editPlan" label-width="auto">
        <el-form-item label="计划编码">
          <el-input v-model="editPlan.planCode"/>
        </el-form-item>
        <el-form-item label="计划名称">
          <el-input v-model="editPlan.planName"/>
        </el-form-item>
        <el-form-item label="计划类型">
          <el-radio-group v-model="editPlan.planType">
            <el-radio value="设备检点计划" size="large">设备检点计划</el-radio>
            <el-radio value="设备保养计划" size="large">设备保养计划</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频率">
          <el-input v-model="editPlan.planRate"/>
        </el-form-item>
        <!--        <el-form-item label="开始时间">-->
        <!--          <el-input v-model="editPlan.startTime"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="结束时间">-->
        <!--          <el-input v-model="editPlan.endTime"/>-->
        <!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间">
              <el-date-picker
                  v-model="editPlan.startTime"
                  type="date"
                  value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间">
              <el-date-picker
                  v-model="editPlan.endTime"
                  type="date"
                  value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态">
          <el-select style="width: 100px" v-model="editPlan.planStatus">
            <el-option :value="1" label="草稿"/>
            <el-option :value="2" label="已确定"/>
            <el-option :value="3" label="审核中"/>
            <el-option :value="4" label="已审核"/>
            <el-option :value="5" label="已完成"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editPlan.planNotes"/>
        </el-form-item>
      </el-form>


      <!-- 工具栏 -->
      <el-row style="margin: 20px">
        <el-col :span="24">
          <el-button type="primary" plain @click="toAdd">
            <el-icon>
              <Plus/>
            </el-icon>
            新增
          </el-button>
          <el-button type="warning" plain @click="toDeletes">
            <el-icon>
              <EditPen/>
            </el-icon>
            删除
          </el-button>
        </el-col>
      </el-row>

      <!-- 数据列表 -->
      <el-row style="margin: 0px 10px;padding: 0px 15px">
        <el-col :span="24">
          <el-table :data="ledgers" border stripe ref="leaderTable">
            <el-table-column type="selection" width="55"/>
            <el-table-column prop="machineCode" label="设备编码"/>
            <el-table-column prop="machineName" label="设备名称"/>
            <el-table-column prop="machineBrand" label="设备品牌"/>
            <el-table-column prop="machineSpec" label="规格型号"/>
            <el-table-column label="操作" width="330">
              <template #default="scope">
                <el-link style="margin: 0px 5px" @click="toDelete(scope.row.machineId)" type="primary">
                  <el-icon>
                    <Delete/>
                  </el-icon>
                  删除
                </el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <el-row style="margin-top: 10px">
        <!-- 监听页码的变化事件 -->
        <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
      </el-row>

    </template>


    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editLedgerSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="addMachineVisible"
             title="设备选择"
             width="50%"
  >

    <!-- 工具栏 -->
    <el-row style="margin: 20px">
      <el-col :span="24">
        <el-button type="primary" plain @click="toEdits">
          <el-icon>
            <Plus/>
          </el-icon>
          添加
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="newLedger" border stripe ref="editTable">
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="machineCode" label="设备编码"/>
      <el-table-column prop="machineName" label="设备名称"/>
      <el-table-column prop="machineBrand" label="设备品牌"/>
      <el-table-column prop="machineSpec" label="规格型号"/>
      <el-table-column label="操作" width="330">
        <template #default="scope">
          <el-link style="margin: 0px 5px" @click="toEdit(scope.row.machineId)" type="primary">
            <el-icon>
              <EditPen/>
            </el-icon>
            添加
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-button style="margin-left: 400px; margin-top: 20px" @click="addMachineVisible =false" newLedger={}>确定</el-button>
  </el-dialog>


</template>

<script setup>

import {ref, watch} from "vue";
import ledgerRequest from "@/api/ledger.js";
import {ElMessage, ElMessageBox} from "element-plus";
import maintainPlanRequest from "@/api/maintainPlan.js";
import {Delete, EditPen} from "@element-plus/icons-vue";

const props = defineProps(['planId']);
const emits = defineEmits(['editComplate']);
let planEditWinDialog = ref(false);
let editPlan = ref({});
let ledgers = ref([]);
let curPage = ref(1);
let total = ref(0);
let leaderTable = ref([]);
let ledgerQuery = ref({});
let addMachineVisible = ref(false);
let newLedger = ref({});
let editTable = ref([]);

function toEdits() {
  let selectionRows = editTable.value.getSelectionRows();
  if (selectionRows.length == 0) {
    ElMessage({
      type: "warning",
      message: "请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要添加吗？',
      'warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    selectionRows.forEach(row => {
      ledgerRequest.updateByDelete(row.machineId, res => {
        if (res.code == 200) {
          ElMessage({
            type: "success",
            message: "添加成功",
          })
        } else {
          ElMessage({
            type: "warning",
            message: "删除成功",
          })
        }
      })
    })
    ledgerRequest.selectByDelete(res => {
      newLedger.value = res.data;
    })
  })
}

function toEdit(machineId) {
  ledgerRequest.updateByDelete(machineId, res => {
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "添加成功"
      })
    } else {
      ElMessage({
        type: "warning",
        message: "添加失败"
      })
    }
  })
  ledgerRequest.selectByDelete(res => {
    newLedger.value = res.data;
  })
}

//工具栏添加
function toAdd() {
  ledgerRequest.selectByDelete(res => {
    newLedger.value = res.data;
    addMachineVisible.value = true;
  })
}

// function loadDelete(){
//   ledgerRequest.selectByDelete(res=>{
//     newLedger.value=res.data;
//   })
// }

//工具栏删除
function toDeletes() {
  let selectionRows = leaderTable.value.getSelectionRows();
  if (selectionRows.length == 0) {
    ElMessage({
      type: "warning",
      message: "请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要删除吗？',
      'warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    selectionRows.forEach(row => {
      ledgerRequest.deleteLedger(row.machineId, res => {
        if (res.code == 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          })
        } else {
          ElMessage({
            type: "warning",
            message: "删除失败",
          })
        }
      })
    })
    loadLedger();
  })
}


function toDelete(machineId) {
  ElMessageBox.confirm(
      '您确定要删除吗',
      'Warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    ledgerRequest.deleteLedger(machineId, res => {
      if (res.code == 200) {
        ElMessage({
          type: "success",
          message: "操作完成"

        })
        loadLedger();
      } else {
        ElMessage({
          type: "warning",
          message: "删除失败"
        })
      }
    })
    loadLedger();
  })
}

function loadLedger() {
  ledgerRequest.loadLedger(ledgerQuery.value, curPage.value, 10, res => {
    total.value = res.data.total;
    ledgers.value = res.data.list;
  })
}

function changePage(value) {
  curPage.value = value;
  loadLedger();
}

function cancel() {
  editPlan = ref({});
  planEditWinDialog.value = false;
}

function editLedgerSubmit() {
  maintainPlanRequest.updatePlan(editPlan.value, res => {
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "修改成功"
      });
      cancel()
      emits('editComplate');
    } else {
      ElMessage({
        type: "warning",
        message: "修改失败"
      })
    }
  })
}

function loadPlanById() {
  maintainPlanRequest.selectById(props.planId, res => {
    if (res.data) {
      editPlan.value = res.data;
      planEditWinDialog.value = true;
    }
  })
}

watch(() => props.planId, () => {
  if (props.planId) {
    loadPlanById();
    loadLedger();
  }
});


</script>

<style scoped>

</style>